<?php
	/** remote upload photo class(client)
	*	Date: 	2011-5-3
	*	Author:	terrywu
	*/
	class RemoteUpload{
	
		/**
		* @param String $file	文件絕對路徑
		* @param String	$dir	保存的文件夾路徑
		* @param Array	$param	其他參數
		*/
		public function uploadToRemote($file,$dir,$param=array()){

			global $site_outsync_config,$site_global_config;

			$LOCALDOMAIN = $site_outsync_config['LOCALDOMAIN'];
			$UPLOAD_TYPE = $site_global_config['UPLOAD_TYPE'];

			if(!file_exists($file)){
				return array('status'=>'fail');
			}
			//server select
			$server = $this->getServer();
			$url = $server['url'];
			$domain = $server['domain'];
			$maxwidth = defined('UPLOAD_MAX_WIDTH')? UPLOAD_MAX_WIDTH : 1280;
			$maxheight = defined('UPLOAD_MAX_HEIGHT')? UPLOAD_MAX_WIDTH : 1280;

			if($UPLOAD_TYPE=='gearman'){
				//geraman to work
				$jobparam = array();
				$jobparam['file'] = $LOCALDOMAIN.str_replace(WWW_PATH,'',$file);
				$jobparam['dir'] = $dir;
				$jobparam['url'] = $url;
				$jobparam['domain'] = $domain;
				$jobparam['maxwidth'] = $maxwidth;
				$jobparam['maxheight'] = $maxwidth;

				$gmc_obj = getLibClass('GMC');
				$response = $gmc_obj->job_do('upload_photo',$jobparam,false);	//false 同步
				return json_decode($response,true);
			
			}else{
			
				//resize file
				$imginfo = getimagesize($file);	
				if(!$imginfo){
					return array('status'=>'fail');
				}
				
				$imghandle = "";
				if($imginfo[0]>$maxwidth || $imginfo[1]>$maxheight){
					$imghandle = $imghandle ." -resize ".(int)$maxwidth."x".(int)$maxheight." ";
				}
				if($imginfo['channels']!=3){
					$imghandle = $imghandle ."  -colorspace rgb ";
				}

				if($imghandle!=''){
					exec("convert  ".$imghandle." '".$file."' '".$file."'");
				}

				$size = filesize($file);

				//post fields
				$fields = array();
				$fields['pic'] = '@'.$file;
				$fields['path'] = $dir;
				$fields['filename'] = basename($file);
		
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($ch, CURLOPT_URL, $url);
				curl_setopt($ch, CURLOPT_POST, true);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
				$response = curl_exec($ch);
				if ($error = curl_error($ch) ) {
					return array('status'=>'fail');
				}
				curl_close($ch);
				$response = json_decode($response,true);
				$response['domain'] = $domain;
				$response['size'] = $size;
				return $response;
			}

		}
		
		/** 選擇圖片服務器 */
		private function getServer(){

			global $IMGSERVER;

			if(count($IMGSERVER)>1){
				//獲取服務器分配情況
				$kvcobj = getCache('server_assign',array('expire'=>0));
				$server_assign = $kvcobj->get('config');

				if(!$server_assign){	//沒有分配則平均分配
					$server_assign = array();
					for($i=1,$max=count($IMGSERVER); $i<=$max; $i++){
						array_push($server_assign,$i);
					}
				}

				//按比例分配隨機選擇圖片服務器
				shuffle($server_assign);
				$sel = rand(0,count($server_assign)-1);
				$flag = $server_assign[$sel];
			}else{
				$flag = 1;
			}

			$server = array();
			$server['url'] = $IMGSERVER['imgserver'.$flag]['url'];			//remote api url
			$server['domain'] = $IMGSERVER['imgserver'.$flag]['domain'];	//domain
			return 	$server;
		}

	}
?>
